package org.yty.travelagencyredis.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yty.travelagencyredis.Dao.GuidebookDao;
import org.yty.travelagencyredis.model.Entity.Guidebook;
import org.yty.travelagencyredis.service.GuidebookService;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class GuidebookServiceImpl implements GuidebookService {
    private final GuidebookDao guidebookDao;

    @Override
    public Guidebook getById(Long id) {
        return this.guidebookDao.selectById(id);
    }

    @Override
    public boolean save(Guidebook guidebook) {
        if (guidebook.getId() == null){
            guidebook.setId(IdUtil.getSnowflakeNextId());
        }
        guidebook.setCreateTime(LocalDateTime.now());
        guidebook.setUpdateTime(LocalDateTime.now());
        return this.guidebookDao.insert(guidebook);
    }

    @Override
    public boolean update(Guidebook guidebook) {
        guidebook.setUpdateTime(LocalDateTime.now());
        return this.guidebookDao.update(guidebook);
    }

    @Override
    public void removeById(Long id) {
        this.guidebookDao.deleteById(id);
    }
}
